package com.jinxin.platform.cdfaceacs.service.impl;

import com.jinxin.platform.cdfaceacs.common.CommonCodeEntity;
import com.jinxin.platform.cdfaceacs.common.ResultEntity;
import com.jinxin.platform.cdfaceacs.pojo.domains.JurenIcCardInfo;
import com.jinxin.platform.cdfaceacs.pojo.entity.Constant;
import com.jinxin.platform.cdfaceacs.pojo.vo.ResultVO2;
import com.jinxin.platform.cdfaceacs.service.BaseService;
import com.jinxin.platform.cdfaceacs.service.IJurenICCardService;
import com.jinxin.platform.cdfaceacs.service.RestTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-07-28
 * @Author: Lingnan
 **/
@Slf4j
@Service
public class JurenICCardServiceImpl extends BaseService implements IJurenICCardService {
    @Autowired
    private RestTemplateService restTemplateService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultEntity saveOrUpdate(JurenIcCardInfo icCardInfo) {
        if(icCardInfo == null){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"参数异常，保存失败","" );
        }
        String cardId = icCardInfo.getCardId();
        if(StringUtils.isEmpty(cardId)){
            ResultVO2 resultVO = restTemplateService.iCCardSubmit(Constant.OPERATION_SIGN_ADD, icCardInfo);
            if(isSucceed(resultVO)){
                return new ResultEntity(CommonCodeEntity.SUC_CODE,"操作成功","success");
            }else {
                log.info("iCCardSubmit add fail: " + resultVO.toString());
                return new ResultEntity(CommonCodeEntity.FAIL,resultVO.getRstDesc(),"fail");
            }
        }else {
            ResultVO2 resultVO = restTemplateService.iCCardSubmit(Constant.OPERATION_SIGN_EDIT, icCardInfo);
            if(isSucceed(resultVO)){
                return new ResultEntity(CommonCodeEntity.SUC_CODE,"操作成功","success");
            }else {
                log.info("iCCardSubmit edit fail: " + resultVO.toString());
                return new ResultEntity(CommonCodeEntity.FAIL,resultVO.getRstDesc(),"fail");
            }
        }
    }
}
